package com.anolesoft.yr.mbus.base.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.base.dto.BaseCompanyDTO;
import com.anolesoft.yr.mbus.base.service.BaseCompanyService;
import com.anolesoft.yr.mbus.base.vo.BaseCompanyVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/9 10:48
 */
@RestController
public class BaseCompanyController {
    @Autowired
    private BaseCompanyService baseCompanyService;

//    private BaseCompanyVO dto2vo(BaseCompanyDTO t) {
//        BaseRegionDTO region = t.getRegion();
//        BaseRegionVO baseRegionVO = null;
//        if (region != null) {
//            baseRegionVO = BaseRegionVO.builder()
//                    .remark(region.getRemark())
//                    .parentUid(StringUtils.isEmpty(region.getParentUid()) ? "-" : region.getParentUid())
//                    .modifyUserUid(region.getModifyUserUid())
//                    .deleteFlag(region.getDeleteFlag())
//                    .createUserUid(region.getCreateUserUid())
//                    .regionCode(region.getRegionCode())
//                    .regionLevel(region.getRegionLevel())
//                    .regionName(region.getRegionName())
//                    .factoryCode(region.getFactoryCode())
//                    .objuid(region.getObjuid())
//                    .build();
//        } else {
//            baseRegionVO = new BaseRegionVO();
//        }
//        List<BaseContactVO> contactVOS = t.getContacts().stream().map(baseContactDTO -> BaseContactVO.builder()
//                .contactNumber(baseContactDTO.getContactNumber())
//                .contactPerson(baseContactDTO.getContactPerson())
//                .createTime(baseContactDTO.getCreateTime())
//                .createUserUid(baseContactDTO.getCreateUserUid())
//                .crmAddress(baseContactDTO.getCrmAddress())
//                .deleteFlag(baseContactDTO.getDeleteFlag())
//                .factoryCode(baseContactDTO.getFactoryCode())
//                .modifyTime(baseContactDTO.getModifyTime())
//                .modifyUserUid(baseContactDTO.getModifyUserUid())
//                .objuid(baseContactDTO.getObjuid())
//                .postcode(baseContactDTO.getPostcode())
//                .remark(baseContactDTO.getRemark())
//                .targetUid(baseContactDTO.getTargetUid())
//                .type(baseContactDTO.getType())
//                .build()).collect(Collectors.toList());
//        return BaseCompanyVO.builder()
//                .account(t.getAccount())
//                .bank(t.getBank())
//                .baseRegionUid(t.getBaseRegionUid())
//                .companyAddress(t.getCompanyAddress())
//                .companyCode(t.getCompanyCode())
//                .companyGrade(t.getCompanyGrade())
//                .companyName(t.getCompanyName())
//                .companyOtherName(t.getCompanyOtherName())
//                .companyType(t.getCompanyType())
//                .companyWebsite(t.getCompanyWebsite())
//                .contactNumber(t.getContactNumber())
//                .contactPerson(t.getContactPerson())
//                .createTime(t.getCreateTime())
//                .createUserUid(t.getCreateUserUid())
//                .deleteFlag(t.getDeleteFlag())
//                .email(t.getEmail())
//                .establishTime(t.getEstablishTime())
//                .factoryCode(t.getFactoryCode())
//                .faxNumber(t.getFaxNumber())
//                .legalPerson(t.getLegalPerson())
//                .modifyTime(t.getModifyTime())
//                .modifyUserUid(t.getModifyUserUid())
//                .objuid(t.getObjuid())
//                .region(baseRegionVO)
//                .remark(t.getRemark())
//                .taxNumber(t.getTaxNumber())
//                .contacts(contactVOS)
//                .regionName(t.getRegion()==null ? null : t.getRegion().getRegionName())
//                .build();
//    }

    @PostMapping(value = "/api/base/company/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(BaseCompanyVO baseCompanyVO) {
        BaseCompanyDTO baseCompanyDTO = vo2dto(baseCompanyVO,true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, baseCompanyService.create(baseCompanyDTO));
    }

    private BaseCompanyDTO vo2dto(BaseCompanyVO baseCompanyVO, boolean newId) {
        return BaseCompanyDTO.builder()
                    .companyAddress(baseCompanyVO.getCompanyAddress())
                    .companyOtherName(baseCompanyVO.getCompanyOtherName())
                    .companyName(baseCompanyVO.getCompanyName())
                    .companyCode(baseCompanyVO.getCompanyCode())
                    .baseRegionUid(baseCompanyVO.getBaseRegionUid())
                    .factoryCode(baseCompanyVO.getFactoryCode())
                    .modifyUserUid(SecurityUtils.getUserId())
                    .taxNumber(baseCompanyVO.getTaxNumber())
                    .remark(baseCompanyVO.getRemark())
                    .objuid(newId ? CommonUtils.fastUUID() : baseCompanyVO.getObjuid())
                    .legalPerson(baseCompanyVO.getLegalPerson())
                    .faxNumber(baseCompanyVO.getFaxNumber())
                    .establishTime(baseCompanyVO.getEstablishTime())
                    .email(baseCompanyVO.getEmail())
                    .createUserUid(SecurityUtils.getUserId())
                    .contactPerson(baseCompanyVO.getContactPerson())
                    .contactNumber(baseCompanyVO.getContactNumber())
                    .companyWebsite(baseCompanyVO.getCompanyWebsite())
                    .companyType(baseCompanyVO.getCompanyType())
                    .companyGrade(baseCompanyVO.getCompanyGrade())
                    .bank(baseCompanyVO.getBank())
                    .account(baseCompanyVO.getAccount())
                    .build();
    }

    @PutMapping(value = "/api/base/company/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(BaseCompanyVO baseCompanyVO) {
        BaseCompanyDTO baseCompanyDTO = vo2dto(baseCompanyVO,false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, baseCompanyService.modify(baseCompanyDTO));
    }

    @DeleteMapping(value = "/api/base/company/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, baseCompanyService.remove(id));
    }

    @DeleteMapping(value = "/api/base/company/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult removeBatch(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, baseCompanyService.removeBatch(ids));
    }

    @GetMapping(value = "/api/base/company/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<BaseCompanyDTO> baseCompanyDTO = this.baseCompanyService.selectOneById(id);
        if (baseCompanyDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.baseCompanyDTO2VO(baseCompanyDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }

    }

    @GetMapping(value = "/api/base/company/list/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll() {
        List<BaseCompanyVO> vos = baseCompanyService.selectAll().stream().map(t -> CustomBeanUtils.baseCompanyDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<BaseCompanyVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/base/company/list/all/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageAll(
            @RequestParam("rows") Integer rows,
            @RequestParam("page") Integer page) {
        //List<BaseCompanyVO> vos = baseCompanyService.selectAll(limit, curr).stream().map(t -> dto2vo(t)).collect(Collectors.toList());

        PageData<List<BaseCompanyDTO>> pageData = baseCompanyService.selectAll(rows, page);
        List<BaseCompanyVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.baseCompanyDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<BaseCompanyVO>> pageInfo = new PageInfo<>(rows, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/base/company/list/condition", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(BaseCompanyVO baseCompanyVO) {
        BaseCompanyDTO baseCompanyDTO = BaseCompanyDTO.builder()
                .factoryCode(baseCompanyVO.getFactoryCode())
                .baseRegionUid(baseCompanyVO.getBaseRegionUid())
                .companyCode(baseCompanyVO.getFactoryCode())
                .companyName(baseCompanyVO.getCompanyName())
                .companyOtherName(baseCompanyVO.getCompanyOtherName())
                .companyAddress(baseCompanyVO.getCompanyAddress())
                .objuid(baseCompanyVO.getObjuid())
                .build();
        List<BaseCompanyVO> vos = this.baseCompanyService.selectList(baseCompanyDTO).stream().map(t -> CustomBeanUtils.baseCompanyDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<BaseCompanyVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/base/company/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectListPage(BaseCompanyVO baseCompanyVO) {
        BaseCompanyDTO baseCompanyDTO = BaseCompanyDTO.builder()
                .factoryCode(baseCompanyVO.getFactoryCode())
                .baseRegionUid(baseCompanyVO.getBaseRegionUid())
                .companyCode(baseCompanyVO.getCompanyCode())
                .companyName(baseCompanyVO.getCompanyName())
                .companyOtherName(baseCompanyVO.getCompanyOtherName())
                .companyAddress(baseCompanyVO.getCompanyAddress())
                .objuid(baseCompanyVO.getObjuid())
                .build();
        Integer limit = baseCompanyVO.getRows();
        Integer curr = baseCompanyVO.getPage();

//        List<BaseCompanyVO> vos = this.baseCompanyService.selectList(baseCompanyDTO, limit, curr).stream().map(t -> dto2vo(t)).collect(Collectors.toList());
//        PageInfo<List<BaseCompanyVO>> pageInfo = new PageInfo<>(limit, vos.size(), vos);
//        return new CommonResult<>(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);

        PageData<List<BaseCompanyDTO>> pageData = this.baseCompanyService.selectList(baseCompanyDTO, limit, curr);
        List<BaseCompanyVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.baseCompanyDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<BaseCompanyVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }
}
